package com.situ.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.web.entity.PageInfo;
import com.situ.web.entity.Student;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.StudentServiceImpl;
import com.situ.web.util.JDBCUtil;

// url-pattern
@WebServlet("/student")
public class StudentServlet extends HttpServlet{
	private IStudentService studentService = new StudentServiceImpl();

	// 访问servlet默认方法service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("StudentServlet.service()");
		req.setCharacterEncoding("utf-8");
//		HttpSession session = req.getSession();
//		User user = (User) session.getAttribute("user");
//		if (user == null) {
//			resp.sendRedirect(req.getContextPath() + "/login.jsp");
//			return;
//		}
		
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "findByPage";
		}
		
		switch (method) {
		case "findAll":
			findAll(req, resp);
			break;
		case "findByPage":
			findByPage(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "findById":
			findById(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "findByName":
			update(req, resp);
			break;
		case "findByAge":
			update(req, resp);
			break;
		case "findByGender":
			update(req, resp);
			break;
		default:
			break;
		}
	}
	
	public void show() {
		//System.out.println("StudentServlet.show()");
		System.out.println("StudentServlet.show()");
	}
	
	public void test() {
		System.out.println();
	}
	
	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageInfo pageInfo = studentService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/student_page_list.jsp").forward(req, resp);;
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), gender);
		boolean isSuccess = studentService.update(student);
		
		// 重定向到查找所有的列表界面
		resp.sendRedirect(req.getContextPath() + "/student?method=findByPage");
	}

	private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String studentId = req.getParameter("id");
		Student student = studentService.findById(Integer.parseInt(studentId));
		
		req.setAttribute("student", student);
		req.getRequestDispatcher("/student_update.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		
		Student student = new Student(name, Integer.parseInt(age), gender);
		boolean isSuccess = studentService.insert(student);
		
		// 重定向到查找所有的列表界面
		resp.sendRedirect(req.getContextPath() + "/student?method=findByPage");
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		boolean isSuccess = studentService.deleteById(Integer.parseInt(id));
		
		// 重定向到查找所有的列表界面
		resp.sendRedirect(req.getContextPath() + "/student?method=findByPage");
	}

	public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Student> list = studentService.findAll();
		
		// 将数据放到request域对象
		req.setAttribute("list", list);
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}
	
}
